package org.wlw.teacher.web;

import org.wlw.teacher.context.SystemContext;

import javax.servlet.*;
import java.io.IOException;

public class SystemContextFilter implements Filter {

    int pageSize = 15;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        pageSize = Integer.parseInt(filterConfig.getInitParameter("pageSize"));
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        int pageIndex = 1;
        try {
            pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
        } catch (NumberFormatException e) {
        }
        //设置了threadlocal
        SystemContext.setPageSize(pageSize);
        SystemContext.setPageIndex(pageIndex);
        chain.doFilter(request, response);
        //清空threadlocal
        SystemContext.clearThreadLocal();
    }

    @Override
    public void destroy() {

    }
}
